package application;

import ij.process.ImageProcessor;

public class EdgeMerge {

	/**
	 * effectue un edge merging avec un step de 15
	 * @param ip
	 * @return
	 */
	public static ImageProcessor run(ImageProcessor ip){
		ImageProcessor result = ip.createProcessor(ip.getWidth(),ip.getHeight());
		for(int i=0;i<ip.getWidth();i++)
		{
			for(int j=0;j<ip.getHeight();j++)
			{
				if (ip.getPixel(i, j) == 255)
				{
					boolean merge = false;
					int k=2;
					while(i+k<ip.getWidth() && k<15 && !merge)
					{
						if(ip.getPixel(i+k, j) == 255)
						{
							merge = true;
						}
						else
							k++;
					}
					if(merge)
					{	
						for(int l=i;l<=i+k;l++)
						{
							result.putPixel(l, j, 255);
						}
					}
					else
						result.putPixel(i, j, ip.getPixel(i, j));
				}
			}
		}
		return result;
	}

	public static ImageProcessor run(ImageProcessor ip, int step) {
		ImageProcessor result = ip.createProcessor(ip.getWidth(),ip.getHeight());
		for(int i=0;i<ip.getWidth();i++)
		{
			for(int j=0;j<ip.getHeight();j++)
			{
				if (ip.getPixel(i, j) == 255)
				{
					boolean merge = false;
					int k=2;
					while(i+k<ip.getWidth() && k<step && !merge)
					{
						if(ip.getPixel(i+k, j) == 255)
						{
							merge = true;
						}
						else
							k++;
					}
					if(merge)
					{	
						for(int l=i;l<=i+k;l++)
						{
							result.putPixel(l, j, 255);
						}
					}
					else
						result.putPixel(i, j, ip.getPixel(i, j));
				}
			}
		}
		return result;
	}
}
